//
//  UIButton+Extension.swift
//  weiboLQ
//
//  Created by 半缘君 on 15/12/7.
//  Copyright © 2015年 banyuanjun.cn. All rights reserved.
//

import UIKit


extension UIButton {
    /// 有文字有背景图片的按钮便利构造函数
    ///
    /// - parameter titile:    标题文字
    /// - parameter imageName: 背景图片名字
    /// - parameter txtColor:  标题文字颜色,默认深灰色
    ///
    /// - returns: 有文字有背景图片的按钮
    convenience init(titile:String, backGroundImage imageName:String,txtColor:UIColor = UIColor.darkGrayColor()){
            self.init()
            setTitle(titile, forState: UIControlState.Normal)
            setTitleColor(txtColor, forState: UIControlState.Normal)
            //        frame.size.height = 30
            setBackgroundImage(UIImage(named:imageName), forState: UIControlState.Normal)
        setBackgroundImage(UIImage(named:imageName + "_highlighted"), forState: UIControlState.Highlighted)
        sizeToFit()
        }
    /// 有图片有文字的按钮便利构造函数
    ///
    /// - parameter title: 文字
    /// - parameter image: 图片
    /// - parameter font:  文字大小,默认12号字
    /// - parameter color: 蚊子颜色,默认深灰色
    ///
    /// - returns:  有图片有文字的按钮
    convenience init(title:String,image:String,font:CGFloat = 12.0,color:UIColor = UIColor.darkGrayColor()){
        self.init()
        setTitleColor(color, forState: UIControlState.Normal)
        setTitle(title, forState: UIControlState.Normal)
        setImage(UIImage(named: image), forState: UIControlState.Normal)
        titleLabel?.font = UIFont.systemFontOfSize(font)
        sizeToFit()
    }
    /// 只有图片的按钮便利构造函数
    ///
    /// - parameter bName: 背景图片名(可以为nil)
    /// - parameter iName: image图片名(可以为nil)
    ///
    /// - returns: 返回有图片有背景的按钮
    convenience init(backImageName:String?,imageName:String?){
        self.init()
        if let imageName = imageName  where imageName != "" {
        setImage(UIImage(named: imageName),
                      forState: UIControlState.Normal)
        setImage(UIImage(named: imageName + "_highlighted"),
                      forState: UIControlState.Highlighted)
        }
        if let backImageName = backImageName where backImageName != "" {
        setBackgroundImage(UIImage(named:backImageName),
                                forState: UIControlState.Normal)
        setBackgroundImage(UIImage(named:backImageName + "_highlighted"),
                                forState: UIControlState.Highlighted)
        }
        sizeToFit()
    }
    /// 有文字有背景色的按钮便利构造函数
    ///
    /// - parameter title:     文字(默认"nil")
    /// - parameter backColor: 背景色 (默认 whiteColor)
    ///
    /// - returns: 有文字有背景色的按钮
    convenience init(title:String?, backColor:UIColor = UIColor.whiteColor()) {
        self.init()
        setTitle(title, forState: UIControlState.Normal)
        backgroundColor = backColor
        sizeToFit()
    }

}


